<template>
    <div class="login-box">
        <div class="imgbox">
            <div class="bgblue"></div>
            <ul class="circles">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <div class="w-full max-w-md">
                <div class="fz48 cof fw">欢迎光临</div>
                <div class="fz14 cof" style="margin-top:10px;line-height:24px;">
                    欢迎来到好玩俱乐部，在这里和志同道合的朋友一起分享有趣的故事，一起组织有趣的活动...
                </div>
            </div>
        </div>
        <div class="loginbox">
            <div class="login-wrap">
                <h1 class="header fz32">{{ title }}</h1>
                <form action="#">
                    <div class="kva-el-items"><input type="text" v-model="loginUser.account" class="kva-login-input"
                                                     placeholder="请输入账号"></div>
                    <div class="kva-el-items"><input type="password" v-model="loginUser.password"
                                                     class="kva-login-input" placeholder="请输入密码"
                                                     @keydown.enter="handleSubmit"></div>
                    <div class="kva-el-items pr">
                        <input type="text" class="kva-login-input" maxlength="6" v-model="loginUser.code"
                               placeholder="请输入验证码" @keydown.enter="handleSubmit">
                        <img class="codeURL" v-if="codeURL" :src="codeURL" alt="" @click="handleGetCaptcha">
                    </div>
                    <div class="kva-el-items" v-loading="btnLoading"><input type="button" @click.prevent="handleSubmit"
                                                                            class="kva-login-btn" value="登录"></div>
                </form>
            </div>
        </div>
    </div>
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {useRouter, useRoute} from "vue-router"
import {GetCaptcha} from "@/api/code.js"
import {useUserStore} from "@/stores/user.js"
import kvaMessage from "@/utils/kvaMessage.js";

// 定义标题
const title = ref("后台管理系统")
// 定义提交按钮的加载属性
const btnLoading = ref(false)
// 定义路由对象
const router = useRouter()
// 获取当前路由信息，用于获取当前路径参数，路径，hash等
const route = useRoute();
// 定义状态对象
const userStore = useUserStore()
// 图形验证码
const codeURL = ref("")
// 获取用户输入的账号和验证码信息
const loginUser = reactive({
    account: "admin",
    password: "123456",
    codeId: "",
    code: "",
})

// 提交表单
const handleSubmit = async () => {
    // 判断是否输入
    if (!loginUser.account) {
        btnLoading.value = false
        kvaMessage.notifyError("请输入账号")
        return;
    }
    if (!loginUser.password) {
        btnLoading.value = false
        kvaMessage.notifyError("请输入密码")
        return;
    }
    if (!loginUser.code) {
        btnLoading.value = false
        kvaMessage.notifyError("请输入验证码")
        return;
    }

    // 使用状态管理存储数据
    try {
        // 开启加载效果
        btnLoading.value = true
        // 登录请求
        await userStore.toLogin(loginUser)
        // 关闭加载效果
        btnLoading.value = false
        // 获取当前路径
        const path = route.query.path || "/"
        // 有回退功能
        router.push(path)
        // 无回退功能（重定向）
        //router.push({path:"/",replace: true})
    } catch (e) {
        btnLoading.value = false
        // 返回的结果是60002，说明登录失败，清空用户输入的数据
        if (e.code === 60002) {
            loginUser.code = ""
            handleGetCaptcha()
        }
    }
}

// 调用验证码的接口
const handleGetCaptcha = async () => {
    const resp = await GetCaptcha()
    const {baseURL, id, answer} = resp.data
    codeURL.value = baseURL
    loginUser.codeId = id
    // loginUser.code = answer // 开发的时候使用验证码code，方便测试
}

// 生命周期
onMounted(() => {
    // 加载验证码
    handleGetCaptcha()
})
</script>

<style scoped lang="scss">
.pr {
    position: relative;
}

.codeURL {
    position: absolute;
    top: 6px;
    right: 5px;
    width: 120px;
}

.kva-el-items {
    margin: 15px 0;
}

.kva-login-input {
    border: 1px solid #eee;
    padding: 16px 8px;
    width: 100%;
    box-sizing: border-box;
    outline: none;
    border-radius: 4px;
}

.kva-login-btn {
    border: 1px solid #eee;
    padding: 16px 8px;
    width: 100%;
    box-sizing: border-box;
    background: #2196F3;
    color: #fff;
    border-radius: 6px;
    cursor: pointer;
}

.kva-login-btn:hover {
    background: #1789e7;
}

.login-box {
    display: flex;
    flex-wrap: wrap;
    background: #fff;

    .loginbox {
        width: 35%;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;

        .header {
            margin-bottom: 30px;
        }

        .login-wrap {
            width: 560px;
            height: 444px;
            padding: 20px 100px;
            box-sizing: border-box;
            border-radius: 8px;
            box-shadow: 0 0 10px #fafafa;
            background: rgba(255, 255, 255, 0.6);
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
    }

    .imgbox {
        width: 65%;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        background: url("../assets/images/login_background.jpg");
        background-size: cover;
        background-repeat: no-repeat;

        .bgblue {
            background-image: linear-gradient(to bottom, #4f46e5, #3b82f6);
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            opacity: 0.75;
        }
    }
}

.circles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.circles li {
    position: absolute;
    display: block;
    list-style: none;
    width: 20px;
    height: 20px;
    background: rgba(255, 255, 255, 0.2);
    animation: animate 25s linear infinite;
    bottom: -150px;
}

.circles li:nth-child(1) {
    left: 25%;
    width: 80px;
    height: 80px;
    animation-delay: 0s;
}

.circles li:nth-child(2) {
    left: 10%;
    width: 20px;
    height: 20px;
    animation-delay: 2s;
    animation-duration: 12s;
}

.circles li:nth-child(3) {
    left: 70%;
    width: 20px;
    height: 20px;
    animation-delay: 4s;
}

.circles li:nth-child(4) {
    left: 40%;
    width: 60px;
    height: 60px;
    animation-delay: 0s;
    animation-duration: 18s;
}

.circles li:nth-child(5) {
    left: 65%;
    width: 20px;
    height: 20px;
    animation-delay: 0s;
}

.circles li:nth-child(6) {
    left: 75%;
    width: 110px;
    height: 110px;
    animation-delay: 3s;
}

.circles li:nth-child(7) {
    left: 35%;
    width: 150px;
    height: 150px;
    animation-delay: 7s;
}

.circles li:nth-child(8) {
    left: 50%;
    width: 25px;
    height: 25px;
    animation-delay: 15s;
    animation-duration: 45s;
}

.circles li:nth-child(9) {
    left: 20%;
    width: 15px;
    height: 15px;
    animation-delay: 2s;
    animation-duration: 35s;
}

.circles li:nth-child(10) {
    left: 85%;
    width: 150px;
    height: 150px;
    animation-delay: 0s;
    animation-duration: 11s;
}

@keyframes animate {
    0% {
        transform: translateY(0) rotate(0deg);
        opacity: 1;
        border-radius: 0;
    }

    100% {
        transform: translateY(-1000px) rotate(720deg);
        opacity: 0;
        border-radius: 50%;
    }
}

.max-w-md {
    max-width: 28rem;
    position: relative;
    z-index: 10;
}
</style>
